package com.alibaba.consumer;

import com.alibaba.dubbo.api.DemoService;
import com.alibaba.dubbo.api.HelloMessage;
import com.alibaba.dubbo.api.HelloRequest;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.remoting.ExecutionException;
import com.alibaba.dubbo.rpc.Protocol;
import com.alibaba.dubbo.util.DubboConfig;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class Consumer {
 
    public static void main(String[] args) throws Exception {
        startByCode();
//        testExtension("registry");
    }

    private static void startBySpring() throws Exception{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
        context.start();

        DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
        String hello = demoService.sayHello("world"); // 执行远程方法

        System.out.println( hello ); // 显示调用结果
    }

    private static void startByCode() throws Exception{
        ApplicationConfig applicationConfig=new ApplicationConfig();
        applicationConfig.setName("my-hello-world-app-consumer");

        String zkAddress= DubboConfig.getZkUrl();
        List<RegistryConfig> registryConfigList=new ArrayList<RegistryConfig>();
        RegistryConfig registryConfig=new RegistryConfig();
        registryConfig.setAddress(zkAddress);
        registryConfig.setProtocol("zookeeper");
        registryConfigList.add(registryConfig);

        List<ProtocolConfig> protocolConfigList=new ArrayList<ProtocolConfig>();
        ProtocolConfig protocolConfig=new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(20880);
        protocolConfig.setAccesslog("true");
        protocolConfigList.add(protocolConfig);
//        protocolConfig.setSerialization("kryo");

        ConsumerConfig consumerConfig=new ConsumerConfig();
        consumerConfig.setApplication(applicationConfig);
        consumerConfig.setTimeout(5000);
        consumerConfig.setLoadbalance("random");
        consumerConfig.setRegistry(registryConfig);

        ReferenceConfig<DemoService> referenceConfig=new ReferenceConfig<DemoService>();
        referenceConfig.setProtocol("dubbo");
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setInterface(DemoService.class);
        referenceConfig.setCache("lru");

        DemoService demoService=referenceConfig.get();
//        System.out.println("invoke start-----------------------");
//        long start = System.currentTimeMillis();
//        String a = demoService.sayHello("hello world");
//        long end = System.currentTimeMillis();
//        System.out.println("invoke time:" + (end - start));
//        System.out.println(a);

        HelloRequest helloRequest=new HelloRequest();
        helloRequest.setName("heihei");
        HelloMessage helloMessage=demoService.hello(helloRequest);
        System.out.println(helloMessage.getMessage());
    }

    public static void testExtension(String extName){
        com.alibaba.dubbo.rpc.Protocol extension =
                (com.alibaba.dubbo.rpc.Protocol) ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class)
                .getExtension(extName);
        if(extension==null) {
            System.out.println("load failed");
        }
    }
}